//
//  TJVistorView.swift
//  TJSinaWeibo
//
//  Created by 唐后能 on 16/4/8.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
// 定义代理.
protocol TJVistorViewDelegate:NSObjectProtocol {

    func registBtnClicked()
    func loginBtnClicked()
}

class TJVistorView: UIView {

    // 定义代理属性.
    weak var delegate:TJVistorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        // 添加子控件.
        setupUI()
        
        // 设置背景颜色.
        backgroundColor = UIColor(red: 237 / 255.0, green: 237 / 255.0, blue: 237 / 255.0, alpha: 1)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:- 公开方法
    //设置其他控制器访客视图的额外信息.
    func setAdditionalInfo(imgName:String,message:String) {
        
        // 隐藏房子.
        houseView.hidden = true
        // 隐藏遮盖.
        coverView.hidden = true
        // 改变转轮的图片.
        wheelView.image = UIImage(named: imgName)
        // 改变文本消息.
        messageLbl.text = message
 
    }
    
    // 旋转转轮视图.
    func startRotate () {
    
        // 1,使用核心动画来旋转.
//        let anim = CABasicAnimation(keyPath: "transform.rotation")
//        // 设置属性.
//         anim.toValue = M_PI * 2
//        anim.repeatCount = MAXFLOAT
//        anim.duration = 20 //注意不要写成repeatDuration.
//        // 设置动画完成后不要移除,---->默认情况下,动画完成后会移除.
//        anim.removedOnCompletion = false
//        // 将动画加到图层上.
//        wheelView.layer.addAnimation(anim, forKey: nil)
        
        
        // 2,用UIView做动画.---->完成后代用自己,搞成死循环.
        // 没有option的,不能控制匀速旋转.
//        UIView.animateWithDuration(3, animations: { () -> Void in
//            self.wheelView.transform = CGAffineTransformRotate(self.wheelView.transform, CGFloat(M_PI))
//            }) { (Bool) -> Void in
//              self.startRotate()
//        }
        // UIViewAnimationOptions.CurveLinear 表示匀速旋转.
        UIView.animateWithDuration(3, delay: 0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
            self.wheelView.transform = CGAffineTransformRotate(self.wheelView.transform, CGFloat(M_PI))
            }) { (Bool) -> Void in
            self.startRotate()
        }
    }
    
    // MARK: - 私有方法
    // 添加vistorView的子控件.
    private func setupUI (){
    
        // 1,添加子控件.
            // 1.1,轮子View.
        addSubview(wheelView)
            // 遮盖.
        addSubview(coverView)
            // 1.2,房子.
        addSubview(houseView)
            // 1.3.message.
        addSubview(messageLbl)
            // 1.4,注册按钮.
        addSubview(registerBtn)
            // 1.5,登录按钮.
        addSubview(loginBtn)
        
        // 2,禁用Autoresizing
        wheelView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLbl.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        // 3,设置约束.-->添加到顶级父视图上,禁用Autoresizing.
            // 3.1,轮子View.
        addConstraint(NSLayoutConstraint(item: wheelView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint( NSLayoutConstraint(item: wheelView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
            // 遮盖.
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
            // 3.2,房子.
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: wheelView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint( NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: wheelView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
            // 3.3.message.
        addConstraint(NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: wheelView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint( NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: wheelView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
         addConstraint(NSLayoutConstraint(item: messageLbl, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 250))

            // 3.4,注册按钮.
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint( NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
     
            // 3.5,登录按钮.
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLbl, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint( NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerBtn, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
  
    }
    
    // 按钮点击事件.
    @objc private func visitorRegistBtnClicked(){
        
        delegate?.registBtnClicked()
    }
    @objc private func visitorLoginBtnClicked(){
        
        delegate?.loginBtnClicked()
    }
    
    // MARK: - 懒加载vistorView的子控件.
    // 1,轮子
    private lazy var wheelView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 遮盖.
    private lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 2,房子,
    private lazy var houseView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 3,message.
    private lazy var messageLbl:UILabel = {
    
        let message = UILabel()
        // 设置label属性.
        message.text = "关注一些人!看看有神马惊喜!"
        message.font = UIFont.systemFontOfSize(18)
        message.numberOfLines = 0 //换行.
        message.textAlignment = NSTextAlignment.Center
        // 返回
        return message
    }()
    
    // 4,注册按钮.
    private lazy var registerBtn:UIButton = {
    
        let btn = UIButton()
        // 设置btn属性.
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        // 为按钮添加点击事件.
        btn.addTarget(self, action: Selector("visitorRegistBtnClicked"), forControlEvents: UIControlEvents.TouchUpInside)
        
        // 返回.
        return btn
    }()
    
    // 5,登录按钮,
    private lazy var loginBtn:UIButton = {
        
        let btn = UIButton()
        // 设置btn属性.
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        // 为按钮添加点击事件.
        btn.addTarget(self, action: Selector("visitorLoginBtnClicked"), forControlEvents: UIControlEvents.TouchUpInside)
        
        // 返回.
        return btn
    }()

}
